<template>
  <div class="login-box">
    <el-row>
      <el-col :span="12" :xs="0"></el-col>
      <el-col :span="12">
        <el-form
          :rules="rules"
          :model="loginForm"
          class="login-form"
          ref="formLogin"
        >
          <h1>Hello</h1>
          <h2>欢迎来到登录页</h2>
          <el-form-item prop="username">
            <el-input :prefix-icon="User" v-model="loginForm.username" />
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              type="password"
              :prefix-icon="Lock"
              show-password="true"
              v-model="loginForm.password"
            />
          </el-form-item>
          <el-form-item>
            <el-button
              class="login-button"
              type="primary"
              plain
              @click="login"
              :loading="buttonLoading"
            >
              登录
            </el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>
<script lang="ts" setup>
import { User, Lock } from '@element-plus/icons-vue'
import { reactive, ref } from 'vue'
import userStore from '@/store/modules/user'
import { useRoute, useRouter } from 'vue-router'
import { ElNotification, FormRules } from 'element-plus'
import { getTime } from '@/utils/time.ts'
interface loginData {
  username: string
  password: string
}
let route = useRoute()
let router = useRouter()
const loginForm = reactive<loginData>({
  username: 'admin',
  password: 'atguigu123',
})
//按钮loading状态控制
let buttonLoading = ref(false)
//表单
let formLogin = ref()
//pinia
let useStore = userStore()
//校验
const rules = reactive<FormRules<loginData>>({
  username: [
    { required: true, message: '用户名不能为空', trigger: 'blur' },
    {
      min: 5,
      message: '用户名最低5位',
      trigger: 'change',
    },
  ],
  password: [
    { required: true, message: '密码不能为空', trigger: 'blur' },
    {
      min: 6,
      message: '密码最低6位',
      trigger: 'change',
    },
  ],
})

//登录操作
const login = async () => {
  //报错全部表达通过再发请求
  await formLogin.value.validate()
  //点击登录加载loading
  buttonLoading.value = true
  try {
    //通过promise来判断是否登录成功
    buttonLoading.value = false
    await useStore.userLogin(loginForm)
    let redirect: string = route.query.redirect as string
    //登陆成功跳转
    router.push(redirect ? redirect : '/')
    ElNotification({
      type: 'success',
      title: getTime(),
      message: '登录成功',
    })
  } catch (error) {
    buttonLoading.value = false
    ElNotification({
      type: 'error',
      title: (error as Error).message,
    })
  }
}
</script>
<style lang="scss" scoped>
.login-box {
  width: 100%;
  height: 100vh;
  background: url(@/assets/images/background.jpg);
  background-size: cover;

  .login-form {
    position: relative;
    width: 80%;
    top: 30vh;
    background: url(@/assets/images/login_form.png);
    background-size: cover;
    padding: 20px;

    h1 {
      color: #fff;
      font-size: 40px;
    }

    h2 {
      color: #fff;
      font-size: 20px;
      margin: 20px 0px;
    }

    .login-button {
      width: 100%;
    }
  }
}
</style>
